#!/usr/bin/env python
from typing import Dict

from ghat.template import template_github_action
import sys
import io
import ruamel.yaml

yaml = ruamel.yaml.YAML()

template_mapping = {
    ".action_templates/e2e-fork-template.yaml": ".github/workflows/e2e-fork.yml",
    ".action_templates/e2e-pr-template.yaml": ".github/workflows/e2e.yml",
    ".action_templates/e2e-single-template.yaml": ".github/workflows/e2e-dispatch.yml",
}


def _prepend_auto_generated_message(github_action: Dict) -> str:
    s = io.StringIO()
    yaml.dump(github_action, s)
    s.seek(0)
    return """
##################################################################################
# 
# This file is automatically generated using templates. Changes to this file
# should happen through editing the templates under .action_templates/*
# Manual edits will be overwritten.
#
##################################################################################

{}""".format(
        s.read()
    )


def main() -> int:
    for template in template_mapping:
        github_action = template_github_action(template)
        with open(template_mapping[template], "w+") as f:
            f.write(_prepend_auto_generated_message(github_action))
    return 0


if __name__ == "__main__":
    sys.exit(main())
